import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { 
  NbAuthComponent, 
  NbAuthJWTToken, 
  NbAuthModule, 
  NbPasswordAuthStrategy,
} from '@nebular/auth';
import { ChatCommonModule } from '@nx-chat/common';
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';

const routes: Routes = [
  {
    path: '',
    component: NbAuthComponent,
    children: [
      {
        path: '',
        component: LoginComponent,
      },
      {
        path: 'login',
        component: LoginComponent,
      },
      {
        path: 'register',
        component: RegisterComponent,
      },
    ]
  }
];

export const AUTH_SERVICE = NbAuthModule.forRoot({
  strategies: [
    NbPasswordAuthStrategy.setup({
      name: 'email',
      baseEndpoint: 'http://localhost:3333/api/auth/',
      login: {
        requireValidToken: false,
        endpoint: 'login',
        method: 'post',
        redirect: {
          success: '/',
          failure: null,
        },
        defaultErrors: ['出错了，请检查 账号/密码 是否输错！'],
        defaultMessages: ['正在登录应用中...'],
      },
      token: {
        class: NbAuthJWTToken,
        key: 'value',
      },
      register: {
        requireValidToken: false,
        endpoint: 'register',
        method: 'post',
        redirect: {
          success: '/',
          failure: null,
        },
        defaultErrors: ['出错了！请检查网络！'],
        defaultMessages: ['正在处理，请稍后...'],
      }
    }),
  ],
  forms: {},
}).providers;

@NgModule({
  imports: [NbAuthModule, HttpClientModule, ChatCommonModule, RouterModule.forChild(routes)],
  declarations: [LoginComponent, RegisterComponent],
})
export class AuthModule {}
